Evaluation¶
Importing libraries¶
In [91]:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import pandas as pd
from keras.models import load_model
import random
import cv2
import matplotlib.pyplot as plt
SEED=42
print('TensorFlow version:', tf.__version__)
TensorFlow version: 2.10.1
Loading models and data¶
In [92]:
test_df = pd.read_pickle('../model/iteration_0/test_df.pkl')
print("test data loaded succesfully")
model_path = "../model/iteration_0/cnn_model.h5"
model = load_model(model_path)
print("Model loaded successfully")
test data loaded succesfully Model loaded successfully
In [93]:
test_df.sample(n=3, random_state=SEED)
Out[93]:
| id | image_path | type_landscape | type_mythological | type_portrait | type_religious | label | label_name | |
|---|---|---|---|---|---|---|---|---|
| 294 | 13340 | ../data/toy_dataset/13340.jpg | False | False | True | False | 2 | portrait |
| 454 | 23395 | ../data/toy_dataset/23395.jpg | True | False | False | False | 0 | landscape |
| 584 | 6767 | ../data/toy_dataset/6767.jpg | True | False | False | False | 0 | landscape |
The data is correctly loaded, now the model will be loaded
In [94]:
model.summary()
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) [(None, 299, 299, 3)] 0
xception (Functional) (None, 10, 10, 2048) 20861480
global_average_pooling2d_2 (None, 2048) 0
(GlobalAveragePooling2D)
dropout_2 (Dropout) (None, 2048) 0
dense_2 (Dense) (None, 4) 8196
=================================================================
Total params: 20,869,676
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) [(None, 299, 299, 3)] 0
xception (Functional) (None, 10, 10, 2048) 20861480
global_average_pooling2d_2 (None, 2048) 0
(GlobalAveragePooling2D)
dropout_2 (Dropout) (None, 2048) 0
dense_2 (Dense) (None, 4) 8196
=================================================================
Total params: 20,869,676
Trainable params: 8,196
Non-trainable params: 20,861,480
_________________________________________________________________
Function to preprocess images¶
In [95]:
IMG_SIZE = (299, 299)
def parse_image(filename):
image = tf.io.read_file(filename)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, IMG_SIZE)
image = keras.applications.xception.preprocess_input(image)
return image
Create grad-cam¶
The first step is the get the correcy layer
In [96]:
from IPython.display import display
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
model_builder = keras.applications.xception.Xception
img_size = (299, 299)
preprocess_input = keras.applications.xception.preprocess_input
decode_predictions = keras.applications.xception.decode_predictions
row = test_df.sample(1, random_state=SEED).iloc[0]
img_path = row['image_path']
label = row['label']
label_name = row['label_name']
img = mpimg.imread(img_path)
plt.imshow(img)
plt.axis('off')
plt.show()
In [97]:
from keras import Model
# 1) Identify the pooling layer in your trained model:
pooling_layer = model.get_layer("global_average_pooling2d_2")
# └── its input tensor is the 10×10×2048 conv-feature-map you want.
# 2) Build a new “grad-model” that returns both
# a) that conv-map, and
# b) your final predictions
grad_model = Model(
inputs = model.inputs,
outputs = [ pooling_layer.input, model.output ]
)
In [98]:
def get_img_array(img_path, size):
# `img` is a PIL image of size 299x299
img = keras.utils.load_img(img_path, target_size=size)
# `array` is a float32 Numpy array of shape (299, 299, 3)
array = keras.utils.img_to_array(img)
# We add a dimension to transform our array into a "batch"
# of size (1, 299, 299, 3)
array = np.expand_dims(array, axis=0)
return array
def make_gradcam_heatmap(img_array, grad_model, pred_index=None):
# Then, we compute the gradient of the top predicted class for our input image
# with respect to the activations of the last conv layer
with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model(img_array)
if pred_index is None:
pred_index = tf.argmax(preds[0])
class_channel = preds[:, pred_index]
# This is the gradient of the output neuron (top predicted or chosen)
# with regard to the output feature map of the last conv layer
grads = tape.gradient(class_channel, last_conv_layer_output)
# This is a vector where each entry is the mean intensity of the gradient
# over a specific feature map channel
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
# We multiply each channel in the feature map array
# by "how important this channel is" with regard to the top predicted class
# then sum all the channels to obtain the heatmap class activation
last_conv_layer_output = last_conv_layer_output[0]
heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]
heatmap = tf.squeeze(heatmap)
# For visualization purpose, we will also normalize the heatmap between 0 & 1
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
return heatmap.numpy()
In [ ]:
img_array = preprocess_input(get_img_array(img_path, size=img_size))
preds = model.predict(img_array)
probs = preds[0]
top_index = np.argmax(probs)
top_confidence = probs[top_index]
print(f"Predicted class index: {top_index} (confidence {top_confidence:.3f})")
heatmap = make_gradcam_heatmap(img_array, grad_model)
plt.matshow(heatmap)
plt.show()
1/1 [==============================] - 0s 29ms/step Predicted class index: 2 (confidence 0.869)
In [105]:
def display_gradcam(img_path, heatmap, alpha=0.4):
# Load the original image
img = keras.utils.load_img(img_path)
img = keras.utils.img_to_array(img)
# Rescale heatmap to a range 0-255
heatmap = np.uint8(255 * heatmap)
# Use jet colormap to colorize heatmap
jet = mpl.colormaps["jet"]
# Use RGB values of the colormap
jet_colors = jet(np.arange(256))[:, :3]
jet_heatmap = jet_colors[heatmap]
# Create an image with RGB colorized heatmap
jet_heatmap = keras.utils.array_to_img(jet_heatmap)
jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))
jet_heatmap = keras.utils.img_to_array(jet_heatmap)
# Superimpose the heatmap on original image
superimposed_img = jet_heatmap * alpha + img
superimposed_img = keras.utils.array_to_img(superimposed_img)
# Display Grad CAM
display(superimposed_img)
display_gradcam(img_path, heatmap)
pairs = test_df[['label', 'label_name']].drop_duplicates(subset='label')
label_map = pairs.set_index('label')['label_name'].to_dict()
def class_prediction(img_array, label_map, label_name):
preds = model.predict(img_array)
probs = preds[0]
top_index = np.argmax(probs)
top_confidence = probs[top_index]
print(f"Predicted class index: {top_index} (confidence {top_confidence:.3f})")
print("Predicted class name:", label_map[top_index])
print("True class name:", label_name)
class_prediction(img_array, label_map, label_name)
1/1 [==============================] - 0s 33ms/step 1/1 [==============================] - 0s 33ms/step Predicted class index: 2 (confidence 0.869) Predicted class name: portrait True class name: portrait
Here it shows clearly that the model focuses on the faces and with this predict the class is portrait
In [107]:
row = test_df.sample(2, random_state=SEED).iloc[1]
img_path = row['image_path']
label = row['label']
label_name = row['label_name']
img_array = preprocess_input(get_img_array(img_path, size=img_size))
heatmap = make_gradcam_heatmap(img_array, grad_model)
plt.matshow(heatmap)
plt.show()
display_gradcam(img_path, heatmap)
class_prediction(img_array, label_map, label_name)
1/1 [==============================] - 0s 28ms/step 1/1 [==============================] - 0s 28ms/step Predicted class index: 0 (confidence 0.882) Predicted class name: landscape True class name: landscape
It is visible that the model is looking in the distance to classify it correctly as a landscape
In [113]:
row = test_df.sample(7, random_state=SEED).iloc[6]
img_path = row['image_path']
label = row['label']
label_name = row['label_name']
img_array = preprocess_input(get_img_array(img_path, size=img_size))
heatmap = make_gradcam_heatmap(img_array, grad_model)
plt.matshow(heatmap)
plt.show()
display_gradcam(img_path, heatmap)
class_prediction(img_array, label_map, label_name)
1/1 [==============================] - 0s 29ms/step 1/1 [==============================] - 0s 29ms/step Predicted class index: 0 (confidence 0.848) Predicted class name: landscape True class name: landscape
Again it is visible that the model is looking in the distance
Next steps¶
The evaluation is kept short on purpose. The goal was to get the gradcam working and inspect a few images. The model performs surpisingly well on a small amount of test data seen. It looks at the correct places. The next iteration should focus on clustering of the data to see if there is a pattern.
In [ ]: